from google.appengine.api.users import User
from impact import dashboard,platform,widget, authorization

def create_user_and_test_data():
	user = User('test@example.com')

	platform.delete_data_for_user(user)

	profile = platform.UserProfile(key_name=user.user_id(), count=0)
	profile.save(user)
	connection = authorization.IntegrationPointInstance(
									type='Test',token='xxx',secret='yyy',scopes=['all'],username='snake',uid='32',
									pic='http://www.sevensidedcube.net/wp-content/uploads/pacman.png')
	connection.save(user)

	project = dashboard.Project(name='Default Project')
	project.save(user)

	dshbrd = dashboard.Dashboard(name='This is a dashboard', project=project)
	dshbrd.save(user)

	wdef = get_widget_registry('facebook_get_friends')
	wins = dashboard.WidgetInstance(state = dashboard.WIDGET_STATE_INIT,
									name = 'a new widget',
									dashboard = dshbrd,
									widget = wdef,
									rect = [0,0,1,1],
									props = '{"text":"hello"}')
	wins.save(user)
	wdef = get_widget_registry('test0')
	wins = dashboard.WidgetInstance(state = dashboard.WIDGET_STATE_READY,
									name = 'test widget 0',
									dashboard = dshbrd,
									widget = wdef,
									rect = [1,0,2,1],
									props = '{"value":32}')
	dashboard.add_default_connection(wins)
	wins.save(user)
	wdef = get_widget_registry('test1')
	wins = dashboard.WidgetInstance(state = dashboard.WIDGET_STATE_READY,
									name = 'test widget 1',
									dashboard = dshbrd,
									widget = wdef,
									rect = [0,1,2,2],
									props = '{"value":100}')
	dashboard.add_default_connection(wins)
	wins.save(user)

	dshbrd2 = dashboard.Dashboard(name='One more at least', project=project)
	dshbrd2.save(user)
	wdef = get_widget_registry('test2')
	wins = dashboard.WidgetInstance(state = dashboard.WIDGET_STATE_INIT,
									name = 'zee widget',
									dashboard = dshbrd2,
									widget = wdef,
									rect = [1,0,1,2],
									props = '{"text":"p2"}')
	dashboard.add_default_connection(wins)
	wins.save(user)

def get_widget_registry(name):
	return widget.WidgetRegistry.get_by_key_name(name)